//
//  ArtistWorkViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-18.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ArtistWorkViewController.h"
#import "ChannelBookCell.h"
#import "BookViewController.h"

@interface ArtistWorkViewController ()

@end

@implementation ArtistWorkViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotWorks:) name:gotArtistWorkNotify object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self showLoadingView];
    [[DataRequest sharedRequest] getArtistWorkWithAritist:[self.artistInfo valueForKey:@"broadcaster_id"]];
    titleLabel.text = [self.artistInfo valueForKey:@"broadcaster"];
}

- (void)gotWorks:(NSNotification *)notification
{
    [self removeloadingView];
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSArray *result = [notification.object valueForKey:@"result"];
        if ([result isKindOfClass:[NSArray class]]) {
            self.worksArray = result;
            [workGridView reloadData];
        }
    }
}

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
{
    return self.worksArray.count;
}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
{
    ChannelBookCell *cell = (ChannelBookCell *)[gridView dequeueReusableCellWithIdentifier:@"ChannelBookCell"];
    if (cell == nil) {
        cell = [[ChannelBookCell alloc] initWithFrame:CGRectMake(0, 0, 96, 128) reuseIdentifier:@"ChannelBookCell"];
        cell.selectionStyle = AQGridViewCellSelectionStyleBlue;
    }
    cell.bookInfo = [self.worksArray objectAtIndex:index];
    [cell updateDisplay];
    return cell;
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    NSDictionary *bookInfo = [self.worksArray objectAtIndex:index];
    BookViewController *bookViewController = [[[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil] autorelease];
    bookViewController.itemid = [bookInfo valueForKey:@"item_id"];
    bookViewController.itemname = [bookInfo valueForKey:@"item_name"];
    
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController pushViewController:bookViewController animated:YES];
    
    [gridView deselectItemAtIndex:index animated:NO];
}

- (IBAction)navBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:workGridView.frame] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_artistInfo release];
    [_worksArray release];
    [titleLabel release];
    [workGridView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [titleLabel release];
    titleLabel = nil;
    [workGridView release];
    workGridView = nil;
    [super viewDidUnload];
}
@end
